<template>
  <div class="login-container">
    <el-form class="myform" :model="form" ref="myform" :rules="rules">
      <h3>iHRM后台登录系统</h3>
      <el-form-item prop="mobile">
        <el-input
          prefix-icon="el-icon-user-solid"
          placeholder="请输入用户名"
          v-model="form.mobile"
        ></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          prefix-icon="el-icon-lock"
          show-password
          placeholder="请输入密码"
          v-model="form.password"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="btn" @click="login" :loading="loading"
          >登录</el-button
        >
      </el-form-item>
      <div class="mark">还没有账号？立即注册</div>
    </el-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
export default {
  data () {
    return {
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              var reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              reg.test(value)
                ? callback()
                : callback(new Error('手机号格式不正确'))
            }
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度范围是 6 ~ 16 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    ...mapActions({
      userlogin: 'user/login'
    }),
    login () {
      this.$refs.myform.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await this.userlogin(this.form)
            const _redirece = this.$route.query._redirece
            if (_redirece) {
              this.$router.push(_redirece)
              this.$message.success('登录成功')
            } else {
              this.$router.push('/')
              this.$message.success('登录成功')
            }
          } catch (err) {
            console.log(err.message)
            console.dir(err)
          } finally {
            this.loading = false
          }
        } else {
          console.log('登录失败' + valid)
        }
      })
    }
  }
}
</script>

<style lang="scss">
.login-container {
  height: 100%;
  background-image: url('../../assets/common/nm6839.jpg');
  background-position: center;
  .myform {
    width: 520px;
    height: 400px;
    padding: 35px 35px 15px 35px;
    position: fixed;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    h3 {
      font-size: 42px;
      font-weight: 100;
      color: #5ba9d9;
      text-align: center;
      letter-spacing: 8px;
    }
    .btn {
      width: 100%;
      height: 60px;
      background: #72c5f0;
      border: none;
      color: #fff;
      font-size: 20px;
    }
    input.el-input__inner {
      background-color: #d3e5ff;
      color: #65a5cd;
    }
    input.el-input__inner {
      height: 50px;
      border-radius: 4px;
    }
    .mark {
      color: #fff;
      text-align: center;
      line-height: 50px;
    }
  }
}
</style>
